//
//  MainViewController.swift
//  Weibo10
//
//  Created by 袁俊华 on 2019/1/11.
//  Copyright © 2019年 袁俊华. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    private lazy var composedButton: UIButton = {
       //自定义的样式按钮
        let button = UIButton()
        button.tintColor = UIColor.whiteColor()
        button.setImage(UIImage(named: "ic_send"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "ic_send"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "ic_bg"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "ic_bg"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        return button

    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        setupComposedButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        //会创建 tabBar 中的所有控制器对应的按钮
        super.viewWillAppear(animated)
        //将撰写的按钮放到最前面
        tabBar.bringSubviewToFront(composedButton)
    }
    
    
    
    
    
}

//MARK: - 设置界面
extension MainViewController{
    
    //设置撰写按钮
    private func setupComposedButton(){
        tabBar.addSubview(composedButton)
        
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count)
        
        composedButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
    }
    
    //添加所有控制器
    private func addChildViewControllers(){
        
    
        tabBar.tintColor = UIColor.orangeColor()
        
        
        addChildViewController(HomeTableViewController(), title: "首页", img: "ic_home")
        addChildViewController(DisCoverTableViewController(), title: "发现", img: "ic_discover")
        addChildViewController(ProfileTableViewController(), title: "", img: "")
        addChildViewController(ProfileTableViewController(), title: "消息", img: "ic_message")
        addChildViewController(ProfileTableViewController(), title: "我的", img: "ic_mine")
        
        
        
    
    }
    
    //添加控制器
    private func addChildViewController(vc: UIViewController, title: String, img: String) {
        
        
        vc.title = title //设置标题
        vc.tabBarItem.image = UIImage(named: img) //设置图标
        
        //导航控制器
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
        
        
    }
}

